package cn.isgrow.gateway.controller;

import cn.isgrow.gateway.repository.MongoRouteDefinitionRepository;
import cn.isgrow.gateway.service.MongoDynamicRouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/gateway/dynamic/")
public class DynamicRouteController {

    @Autowired private MongoRouteDefinitionRepository mongoRouteDefinitionRepository;
    @Autowired private MongoDynamicRouteService mongoDynamicRouteService;

    /**
     * 查询所有路由信息
     * @return
     */
    @GetMapping("/list")
    public  Flux<RouteDefinition> list() {
        return  mongoRouteDefinitionRepository.getRouteDefinitions();
    }

    @GetMapping("/refresh")
    public  Mono<Void> refresh() {
        return mongoDynamicRouteService.refresh();
    }

}